<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

// function otx_cookie_init()
// {
// 	$GLOBALS['_XCOOKIE'] = array();

// 	$prefix = otx_config_string('cookie_prefix');
// 	$length = strlen($prefix);

// 	foreach ($_COOKIE as $key => $value) {
// 		if (substr($key, 0, $length) == $prefix) {
// 			$name = substr($key, $length);
// 			$GLOBALS['_XCOOKIE'][$name] = $value;
// 			unset($_COOKIE[$key]);
// 		}
// 	}
// }

function otx_cookie_set($name, $value, $lifetime = 0)
{
	if (null !== $value) {
		setcookie(otx_config_string('system_cookie_prefix') . '_' . $name,
				$value,
				($lifetime ? otx_time_stamp() + $lifetime : 0),
				otx_config_string('system_cookie_path'),
				otx_config_string('system_cookie_domain'));
		$GLOBALS['_XCOOKIE'][$name] = $value;
	} else {
		setcookie(otx_config_string('system_cookie_prefix') . '_' . $name, '', -86400 * 365);
		unset($GLOBALS['_XCOOKIE'][$name]);
	}
}

function otx_cookie_get($name, $default = null)
{
	if (isset($GLOBALS['_XCOOKIE'][$name])) {
		return $GLOBALS['_XCOOKIE'][$name];
	} else {
		return $default;
	}
}

function otx_cookie_delete($name)
{
	otx_cookie_set($name, null);
}

function otx_cookie_exists($name)
{
	if (isset($GLOBALS['_XCOOKIE'][$key])) {
		return $GLOBALS['_XCOOKIE'][$key];
	} else {
		return $default;
	}
}